package com.fxwater.admin.controller.biz;

import com.fxwater.biz.domain.ImportantCustomerInfo;
import com.fxwater.biz.service.ImportantCustomerService;
import com.fxwater.common.annotation.Log;
import com.fxwater.common.core.controller.BaseController;
import com.fxwater.common.core.domain.AjaxResult;
import com.fxwater.common.core.page.TableDataInfo;
import com.fxwater.common.enums.BusinessType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/biz/importantCustomer")
public class ImportantCustomerController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(PumpStationController.class);

    private String prefix = "biz/importantCustomer";

    @Autowired
    private ImportantCustomerService importantCustomerService;

    /**
     * 重要客户数据信息列表
     * @param importantCustomerInfo
     * @return
     */
    @RequiresPermissions("biz:importantCustomer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list (ImportantCustomerInfo importantCustomerInfo) {
        startPage();
        List<ImportantCustomerInfo> list = importantCustomerService.selectImportantCustomerInfoList(importantCustomerInfo);
        return getDataTable(list);
    }

    /**
     * 跳转页面
     * @return
     */
    @RequiresPermissions("biz:importantCustomer:view")
    @GetMapping()
    public String importantCustomer()
    {
        logger.info("重要客户");
        return prefix + "/importantCustomer";
    }

    /**
     * 查看详情
     * @return
     */
    @GetMapping("/detail/unit/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap) {
        ImportantCustomerInfo importantCustomerInfo = new ImportantCustomerInfo();
        importantCustomerInfo.setId(Long.parseLong(id));
        mmap.put("importantCustomerInfo", importantCustomerService.selectImportantCustomerInfoList(importantCustomerInfo).get(0));
        return prefix +"/detail";
    }

    /**
     * 跳转修改页面
     * @param mmap
     * @return
     */
    @GetMapping("edit/{importantCustomerId}")
    public String edit ( ModelMap mmap,@PathVariable("importantCustomerId") String importantCustomerId) {
        ImportantCustomerInfo importantCustomerInfo = new ImportantCustomerInfo();
        importantCustomerInfo.setId(Long.parseLong(importantCustomerId));
        importantCustomerInfo =  importantCustomerService.selectImportantCustomerInfoList(importantCustomerInfo).get(0);
        mmap.addAttribute("importantCustomerInfo", importantCustomerInfo);
        return prefix + "/edit";
    }

    /**
     * 保存重要客户修改信息
     * @return
     */
    @RequiresPermissions("biz:importantCustomer:edit")
    @Log(title = "重要客户", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult editSave(ImportantCustomerInfo importantCustomerInfo) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = simpleDateFormat.format(date);
        importantCustomerInfo.setUpdateTime(nowTime);
        return toAjax(importantCustomerService.editSaveImportantCustomer(importantCustomerInfo));
    }

    /**
     * 增加重要客户
     * @param mmap
     * @return
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 添加重要客户
     * @param importantCustomerInfo
     * @return
     */
    @RequiresPermissions("biz:importantCustomer:add")
    @Log(title = "重要客户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ImportantCustomerInfo importantCustomerInfo)
    {
        return toAjax(importantCustomerService.addImportantCustomer(importantCustomerInfo));
    }

    /**
     * 删除
     * @return
     */
    @RequestMapping("/remove")
    @ResponseBody
    @RequiresPermissions("biz:importantCustomer:remove")
    @Log(title = "重要客户删除", businessType = BusinessType.DELETE)
    public AjaxResult remove(String ids) {
        return toAjax(importantCustomerService.removeImportantCustomer(ids));
    }

    /**
     * 修改息状态
     * @return
     */
    @RequestMapping("/changeStatus")
    @RequiresPermissions("biz:importantCustomer:edit")
    @ResponseBody
    public AjaxResult changeStatus(String id,String status) {
        ImportantCustomerInfo importantCustomerInfo = new ImportantCustomerInfo();
        importantCustomerInfo.setId(Long.parseLong(id));
        importantCustomerInfo.setIsValid(status);
        return toAjax(importantCustomerService.editSaveImportantCustomer(importantCustomerInfo));
    }

}
